package mk.com.ldm.corepoint.implementation.client.view;

import java.util.HashMap;
import java.util.LinkedList;

import mk.com.ldm.corepoint.implementation.shared.MenuObject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MenuGenerator {
	LinkedList<MenuObject> menu;
	private String token;
	private String activeToken = "Transformer";
	private String activeSection = "Transformer/WorkFlow";
	HashMap<String, HorizontalPanel> sectionCache = new HashMap<String, HorizontalPanel>();
	VerticalPanel menuSection = new VerticalPanel();
	FlexTable menuPanel = new FlexTable();
	HorizontalPanel sectionPanel = new HorizontalPanel();
	
	public MenuGenerator(){
		this.token = History.getToken();
	}
	
	public void setList(LinkedList<MenuObject> menu){
		this.menu = menu;
	}
	
	@SuppressWarnings("deprecation")
	public VerticalPanel show(){
		
		menuPanel.setStyleName("lt-Menu");
		sectionPanel.setStyleName("lt-Sections");
		
		// dummy object, to render the last object of the menu
		menu.add(new MenuObject("branch","","",""));
		// !!
		
		String currentBranch = new String();
		HorizontalPanel currentSection = new HorizontalPanel();
		
		int menuCount = 0;
		
		for(final MenuObject m:menu){
			if(m.getType().equals("branch")){
				if(currentBranch != null){
					sectionCache.put(currentBranch, currentSection);
					currentSection = new HorizontalPanel();
					currentBranch = m.getLink();
				} else {
					currentBranch = m.getLink();
				}
				
				final Button branch = new Button(m.getName());
				if(!m.getName().isEmpty()){
					branch.addClickHandler(new ClickHandler(){
						@Override
						public void onClick(ClickEvent event) {
							History.newItem(m.getLink()+"/WorkFlow");
							format(m.getLink());
							changeSection(m.getLink());
						}
					});
					
					menuPanel.setWidget(0, menuCount, branch);
					menuPanel.getFlexCellFormatter().getElement(0, menuCount).setId(m.getLink());
					menuCount++;
				}
			} else if(m.getType().equals("item")){
				final Hyperlink link = new Hyperlink(m.getName(), m.getLink());
				menuPanel.setWidget(0, menuCount, link);
				link.addClickHandler(new ClickHandler(){
					@Override
					public void onClick(ClickEvent event) {
						changeSection("");
						format(m.getLink());
					}
				});
				menuPanel.getFlexCellFormatter().getElement(0, menuCount).setId(m.getLink());
				menuCount++;
			} else if(m.getType().equals("child")){
				final Hyperlink sectionLink = new Hyperlink(m.getName(), m.getLink());
				sectionLink.getElement().setId(m.getLink()+"-section");
				currentSection.add(sectionLink);
			}
		}
		
		menuSection.add(menuPanel);
		menuSection.add(sectionPanel);
		
		return menuSection;
	}
	
	public void format(String token){
		if(!activeToken.isEmpty()){
			DOM.getElementById(activeToken).setClassName("lt-Tab");
			if(!activeSection.isEmpty()){
				DOM.getElementById(activeSection).setClassName("gwt-Hyperlink");
			}
		}
		if(sectionCache.get(token.split("/")[0]) != null) {	
			activeToken = token;
			activeSection = token + "-section";
		} else if(token.split("/")[1] != null){
			activeToken = token; 
		}
		
		DOM.getElementById(activeToken).setClassName("lt-Tab-active");
		if(sectionPanel.isVisible()) DOM.getElementById(activeSection).setClassName("lt-Section-active");
	}
	
	public void changeSection(String sectionName){
		sectionPanel.clear();
		if(sectionName.isEmpty()){
			sectionPanel.setVisible(false);
		} else {
			HorizontalPanel section = sectionCache.get(sectionName);
			if(section != null){
				sectionPanel.add(section);
				sectionPanel.setVisible(true);
				format(sectionName);
			}
		
		}
	}
	
	public boolean isEmpty(){
		if(menu == null || menu.isEmpty()) return true;
		return false;
	}
	
}
